package cn.snmp.mutual.filter;

import cn.snmp.mutual.constants.SnmpConstante;
import lombok.extern.slf4j.Slf4j;
import cn.snmp.mutual.data.ObjService;

/**
 * @author dengkn
 * mib 下发可以配置过滤器节点默认实现isFilter（）
 * */
@Slf4j
public abstract class AbstractFilter {

	protected ObjService tableInfo = null;
	protected ObjService tableStruct = null;

	public void init(ObjService tableInfo) {
		this.tableInfo = tableInfo;
	}

	public void setTableStruct(ObjService tableStruct) {
		this.tableStruct = tableStruct;
	}

	public void reset() {
	}

	public static AbstractFilter getFilter(ObjService tableInfo) {
		AbstractFilter tableRowFilter = null;
		String filterClass = tableInfo.getStringValue(SnmpConstante.FILTER);

		try {
			if ("".equals(filterClass) && tableInfo.objectSize(SnmpConstante.FILTER) > 0) {
				filterClass = tableInfo.objectAt(SnmpConstante.FILTER, 0).getStringValue("Class");
			}

			if ("".equals(filterClass)) {
				tableRowFilter = null;
			} else {
				tableRowFilter = (AbstractFilter) Class.forName(filterClass).newInstance();
				tableRowFilter.setTableStruct(tableInfo);
			}

			return tableRowFilter;
		} catch (Exception e) {
			log.error("Filter class error!", e);
			return tableRowFilter;
		}
	}

	/**
	 * Is Filter
	 * @param tableInfo tableInfo
	 * @param rowSet ObjService
	 * @return true/false
	 * */
	public abstract boolean isFilter(ObjService tableInfo, ObjService rowSet);
}